﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class admin_newsadd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
                BindData();
            }
            imgPicUrl.ImageUrl = hfPicUrl.Value;

            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "ckfinder/";
            _FileBrowser.SetupCKEditor(txbContent);
        }

        private void BindData()
        {
            BindTopFlag();
            BindNewsCatogory();
            BindNoticeVisiable();
        }

        private void BindNoticeVisiable()
        {
            this.noticefail.Visible = false;
            this.noticesuccess.Visible = false;
        }
        /// <summary>
        /// 绑定TopFlag 从0到100; 
        /// 0为不置顶,其余从100-1倒序
        /// </summary>
        private void BindTopFlag()
        {
            //List<int> nddlTop = new List<int>();
            //for (int i = 0; i <= 100; i++)
            //    nddlTop.Add(i);
            //ddlTop.DataSource = nddlTop;
            //ddlTop.DataBind();
        }

        /// <summary>
        /// 绑定新闻分类
        /// </summary>
        private void BindNewsCatogory()
        {
            List<NewsCategoryInfo> list = new List<NewsCategoryInfo>();
            list = NewsCategoryInfoLogic.GetAdminNewsCategoryInfo();//GetAllNewsCategoryInfo(0);
            if (list == null)
            {
                list.Add(new NewsCategoryInfo());
            }
            ddlCategory.DataSource = list;
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataBind();
        }

        /// <summary>
        /// 将网页上的控件信息转换成一个实体
        /// </summary>
        /// <returns></returns>
        NewsInfo createNewsInfo()
        {
            NewsInfo news = new NewsInfo();

            news.Title = txbTitle.Text.ToString().Replace("'","''");
            news.HotFlag = 0;
            news.NewFlag = 1;
            news.NewsContent = txbContent.Text.ToString().Replace("'","''");
            if (txbBrief.Text.Trim().Length <= 0)
            {
                news.NewsBrief = null;
            }
            else
            {
                news.NewsBrief = txbBrief.Text.Trim().Replace("'", "''");
            }
            try
            {
                news.PublishTime = Convert.ToDateTime(txbTime.Text);
            }
            catch {
                news.PublishTime = DateTime.Now;
            }
            news.PublishTime = DateTime.Now;
            if (hfPicUrl.Value == "")
                news.NewsPicURL = "images/news-no.gif";
            else
                news.NewsPicURL = hfPicUrl.Value;
            try
            {
                news.TopFlag = ckbTop.Checked ? 1 : 0;
                news.CategoryID = Int32.Parse(ddlCategory.SelectedValue);
            }
            catch
            {
                news.TopFlag = 0;
                //此处发生异常应该立即停止....
                news.CategoryID = 1;
            }
            return news;
        }

        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            NewsInfo news = createNewsInfo();
            if (NewsInfoLogic.AddNewsInfo(news) == 1 && NewsCategoryInfoLogic.SetCategoryHasNews(news.CategoryID) == 1)
            {
                noticesuccess.Visible = true;
                noticefail.Visible = false;
            }
            else{
                noticefail.Visible = true;
                noticesuccess.Visible = false;
            }
            ScriptManager.RegisterStartupScript(this, this.GetType(), "newsaddresult", "AjaxChange();", true);
        }
    }
}
